<?php
/**
 * Created by PhpStorm.
 * User: lawrence
 * Date: 09.06.2014
 * Time: 21:55
 */

class Register_User extends CI_Controller
{
    function get_data()
    {
        $username = $this->input->post('username');
        $email = $this->input->post('useremail');
        $password = $this->input->post('password');
        $secpass = $this->input->post('reppassword');

        $this->load->model('db_model_user');

        if($this->db_model_user->check_username($username))
        {
            echo '<script language="javascript">';
            echo 'alert("Username already exists in database")';
            echo '</script>';

            $this->reload_register();

            die();
        }

        if($this->db_model_user->check_email($email))
        {
            echo '<script language="javascript">';
            echo 'alert(Email already exists in database)';
            echo '</script>';

            $this->reload_register();

            die();
        }

        if($password != $secpass)
        {
            echo '<script language="javascript">';
            echo 'alert("Passwords don\'t match")';
            echo '</script>';

            $this->reload_register();

            die();
        }

        $this->load->library('session');

        $this->db_model_user->register($username, $password, $email);

        $this->db_model_user->login($username, $password);

        $data['window'] = 'home';
        $data['pagetitle'] = 'Home';
        $this->load->view('header', $data);

        $this->load->view('home');

        $this->load->view('footer');
    }

    function reload_register()
    {
        $data['window'] = 'home';
        $data['pagetitle'] = 'Register';
        $this->load->view('header', $data);

        $this->load->view('register');

        $this->load->view('footer');
    }
} 